package com.zhf.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.zhf.base.BaseServlet;
import com.zhf.pojo.Course;
import com.zhf.service.CourseService;
import com.zhf.service.impl.CourseServiceImpl;
import com.zhf.utils.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/28 19:18
 */
@WebServlet("/course")
public class CourseServlet extends BaseServlet {

    /**
     * 查询课程信息列表
     *
     * @param request
     * @param response
     */
    public void findCourseList(HttpServletRequest request, HttpServletResponse response) {
        try {
            //接收参数
            //业务处理
            CourseService courseService = new CourseServiceImpl();
            List<Course> courseList = courseService.findCourseList();
            //结果处理
            //SimplePropertyPreFilter 指定要转换的JSON字段
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,
                    "id", "course_name", "price", "sort_num", "status");
            String result = JSON.toJSONString(courseList, filter);
            //响应结果
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据条件查询课程信息
     *
     * @param request
     * @param response
     */
    public void findByCourseNameAndStatus(HttpServletRequest request, HttpServletResponse response) {
        try {
            //接收参数
            String course_name = request.getParameter("course_name");
            String status = request.getParameter("status");

            //业务处理
            CourseService courseService = new CourseServiceImpl();
            List<Course> courseList = courseService.findByCourseNameAndStatus(course_name, status);

            //处理结果
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name", "price", "sort_num", "status");
            String s = JSON.toJSONString(courseList, filter);

            //返回结果
            response.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据课程id查询课程信息
     *
     * @param request
     * @param response
     */
    public void findCourseById(HttpServletRequest request, HttpServletResponse response) {
        try {
            //接收参数
            String id = request.getParameter("id");

            //业务处理
            CourseService courseService = new CourseServiceImpl();
            Course course = courseService.findCourseById(Integer.parseInt(id));

            //返回结果
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name", "brief", "teacher_name", "teacher_info", "price", "price_tag", "discounts", "preview_first_field", "preview_second_field", "course_img_url", "share_title", "share_description", "course_description", "share_image_title");
            String result = JSON.toJSONString(course, filter);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改课程状态
     *
     * @param request
     * @param response
     */
    public void updateCourseStatus(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取参数
            int id = Integer.parseInt(request.getParameter("id"));
            //业务处理
            CourseService courseService = new CourseServiceImpl();
            //根据id获取课程信息
            Course course = courseService.findCourseById(id);
            //判断课程状态，进行取反设置
            int status = course.getStatus();
            if (status == 0) {
                course.setStatus(1);
            } else {
                course.setStatus(0);
            }
            //获取更新时间
            String dateFormart = DateUtils.getDateFormart();
            course.setUpdate_time(dateFormart);
            //保存修改后的状态
            Map<String, Integer> map = courseService.updateCourseStatus(course);
            //响应请求
            String result = JSON.toJSONString(map);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
